package com.vhly.apb;

import android.database.Cursor;
import android.provider.Contacts;
import com.vhly.remoteapi.yahoo.contact.Contact;
import com.vhly.remoteapi.yahoo.contact.Field;
import com.vhly.remoteapi.yahoo.contact.FieldType;
import com.vhly.remoteapi.yahoo.contact.Status;
import com.vhly.remoteapi.yahoo.contact.field.TextField;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-13
 * Time: 21:50:04
 * Email: vhly@163.com
 */
public class ContactCursorParser {
    public static Contact parse(Cursor cur) {
        Contact ret = null;
        if (cur != null) {
            ret = new Contact();
            int ni = cur.getColumnIndex(Contacts.People.DISPLAY_NAME);
            if (!cur.isNull(ni)) {
                Field fd = new Field();
                fd.type = FieldType.nickname;
                fd.value = new TextField(cur.getString(ni));
                fd.status = Status.ADD;
                ret.fields.add(fd);
                ni = cur.getColumnIndex(Contacts.People.NUMBER);
                if(!cur.isNull(ni)){
                    fd = new Field();
                    fd.type = FieldType.phone;
                    fd.value = new TextField(cur.getString(ni));
                    fd.status = Status.ADD;
                    ret.fields.add(fd);
                }
                ni = cur.getColumnIndex(Contacts.People.PRIMARY_EMAIL_ID);
                if(!cur.isNull(ni)){
                    fd = new Field();
                    fd.type = FieldType.email;
                    fd.value = new TextField(cur.getString(ni));
                    fd.status = Status.ADD;
                    ret.fields.add(fd);
                }
            }
        }
        return ret;
    }
}
